/**
 * 
 */
package com.tw.trips.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import org.springframework.stereotype.Service;

/**
 * @author M1005264
 *
 */
@Service
public class HttpUtil {

	
	public String sendGetRequest(String endpoint,
			String requestParameters) {

		String result = null;
		if (endpoint.startsWith("http://")) {
			// Send a GET request to the servlet
			try {
				System.setProperty("http.proxyHost", "internetmcc");
				System.setProperty("http.proxyPort", "8085");
				// Send data
				String urlStr = endpoint;
				if (requestParameters != null && requestParameters.length() > 0) {
					urlStr += "?" + requestParameters;
				}
				URL url = new URL(urlStr);
				URLConnection conn = url.openConnection();
				conn.setRequestProperty("content-type", "application/text");
				conn.setDoInput(true);
				conn.setDoOutput(true);
				// Get the response
				BufferedReader rd = new BufferedReader(new InputStreamReader(
						conn.getInputStream()));
				StringBuffer sb = new StringBuffer();
				String line;
				while ((line = rd.readLine()) != null) {
					sb.append(line);
				}
				rd.close();
				result = sb.toString();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	
		
	}
}
